package com.applet.handler;

import com.alibaba.fastjson.JSON;
import com.applet.pojo.entity.Activity;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author “XuShao”
 * @date 2024/12/20 14:50
 * @description
 */

// 定义这个类型处理器处理的Java类型为List<String>
@MappedTypes(List.class)
// 定义这个类型处理器处理的JDBC类型为ARRAY
@MappedJdbcTypes(JdbcType.ARRAY)
@Slf4j
@Component
public class ListStringTypeHandler extends BaseTypeHandler<List<String>> implements MetaObjectHandler {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        // 将List<String>转换为JSON字符串（这里使用简单的拼接，实际可以使用JSON库）
        String jsonStr = JSON.toJSONString(parameter);
        log.info("setNonNullParameter ==> {}", jsonStr);
        ps.setString(i, jsonStr);
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        log.info("getNullableResult1 ==>");
        String jsonStr = rs.getString(columnName);
        // 将JSON字符串转换回List<String>（这里简单解析，实际使用JSON库更好）
        List<String> list = new ArrayList<>();

        list = JSON.parseObject(jsonStr, List.class);

        log.info("getNullableResult1 ==> {}", list);
        return list;
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        log.info("getNullableResult2 ==>");
        String jsonStr = rs.getString(columnIndex);
        List<String> list = new ArrayList<>();
        if (jsonStr != null && jsonStr.startsWith("[") && jsonStr.endsWith("]")) {
            jsonStr = jsonStr.substring(1, jsonStr.length() - 1);
            String[] strs = jsonStr.split(",");
            for (String str : strs) {
                list.add(str.trim());
            }
        }
        return list;
    }

    @Override
    public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        log.info("getNullableResult3 ==>");
        String jsonStr = cs.getString(columnIndex);
        List<String> list = new ArrayList<>();
        if (jsonStr != null && jsonStr.startsWith("[") && jsonStr.endsWith("]")) {
            jsonStr = jsonStr.substring(1, jsonStr.length() - 1);
            String[] strs = jsonStr.split(",");
            for (String str : strs) {
                list.add(str.trim());
            }
        }
        return list;
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        // 插入数据时的填充逻辑，如果需要可以在这里设置默认值等
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新数据时的填充逻辑
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

    }
}
